package jp.co.sharp.android.parents.kidsguard.activity.bluetooth.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import jp.co.sharp.android.parents.kidsguard.R;
import jp.co.sharp.android.parents.kidsguard.dto.BluetoothItemDto;

public class BluetoothItemKnowAdapter extends ArrayAdapter<BluetoothItemDto>{

    private int resourceId;
    private OnDelBluetoothListenter mOnDelBluetoothListenter;

    public BluetoothItemKnowAdapter(@NonNull Context context, int resource, OnDelBluetoothListenter pOnDelBluetoothListenter) {
        super(context, resource);
        this.resourceId = resource;
        this.mOnDelBluetoothListenter = pOnDelBluetoothListenter;
    }

    public BluetoothItemKnowAdapter(@NonNull Context context, int resource, int textViewResourceId, OnDelBluetoothListenter pOnDelBluetoothListenter) {
        super(context, resource, textViewResourceId);
        this.resourceId = resource;
        this.mOnDelBluetoothListenter = pOnDelBluetoothListenter;
    }

    public BluetoothItemKnowAdapter(@NonNull Context context, int resource, @NonNull BluetoothItemDto[] objects, OnDelBluetoothListenter pOnDelBluetoothListenter) {
        super(context, resource, objects);
        this.resourceId = resource;
        this.mOnDelBluetoothListenter = pOnDelBluetoothListenter;
    }

    public BluetoothItemKnowAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull BluetoothItemDto[] objects, OnDelBluetoothListenter pOnDelBluetoothListenter) {
        super(context, resource, textViewResourceId, objects);
        this.resourceId = resource;
        this.mOnDelBluetoothListenter = pOnDelBluetoothListenter;
    }

    public BluetoothItemKnowAdapter(@NonNull Context context, int resource, @NonNull List<BluetoothItemDto> objects, OnDelBluetoothListenter pOnDelBluetoothListenter) {
        super(context, resource, objects);
        this.resourceId = resource;
        this.mOnDelBluetoothListenter = pOnDelBluetoothListenter;
    }

    public BluetoothItemKnowAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<BluetoothItemDto> objects, OnDelBluetoothListenter pDelBluetoothListenter) {
        super(context, resource, textViewResourceId, objects);
        this.resourceId = resource;
        this.mOnDelBluetoothListenter = pDelBluetoothListenter;
    }
    @Override
    public View getView(int position, View converView, ViewGroup parent) {
        BluetoothItemDto bluetoothItemDto = getItem(position);

        View view;
        ViewHolder viewHolder;
        if (null == converView) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.deviceNickName = view.findViewById(R.id.knowBluetoothItem);
            viewHolder.delBluetoothBtn = view.findViewById(R.id.delKnowBluetoothItem);

            view.setTag(viewHolder);
        } else {
            view = converView;
            viewHolder = (ViewHolder)view.getTag();
        }
        viewHolder.deviceNickName.setText(bluetoothItemDto.getDeviceNickName());
        viewHolder.delBluetoothBtn.setOnClickListener(new DelBluetoothListenter());
        viewHolder.delBluetoothBtn.setTag(position);
        return view;
    }

    class ViewHolder{
        TextView deviceNickName;
        ImageView delBluetoothBtn;
    }

    /**
     *
     */
    public interface OnDelBluetoothListenter {
        void delBluetoothClick(int position, View view);
    }

    /*
     * 对外公开方法让Activity实现
     */
    public void OnDelBluetoothListenter(OnDelBluetoothListenter callBack) {
        mOnDelBluetoothListenter = callBack;
    }

    class DelBluetoothListenter implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            mOnDelBluetoothListenter.delBluetoothClick((Integer)view.getTag(), view);
        }
    }
}
